/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Lid;
import model.Rit;
import org.hibernate.Session;
import org.hibernate.Transaction;
import persistence.SessionManager;

/**
 *
 * @author Johan
 */
public class MijnInschrijvingenController extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        // Haal de huidige sessie op
        HttpSession sessie = request.getSession();

        //huidige lid uit de sessie halen
        Lid currentLid = (Lid) sessie.getAttribute("currentUser");

        //hibernate session
        Session session = null;
        session = SessionManager.openSession();
        Transaction tx = session.beginTransaction();

        //data voor de minimap      
        try {
            int ritnummer  = Integer.parseInt(request.getParameter("minimapinfo")); 
            Rit rit = (Rit) session.load(Rit.class, new Integer(ritnummer));
            request.setAttribute("googleVan", GetGoogleString(rit.getRitVanPlaats(), rit.getRitVanStraat()));
            request.setAttribute("googleNaar", GetGoogleString(rit.getRitNaarPlaats(), rit.getRitNaarStraat()));
        } catch (Exception e) {
        }



        List<Rit> Ritten = new ArrayList<Rit>();
        List<Rit> AcceptedRitten = new ArrayList<Rit>();

        String sqlquery = "SELECT R.rit_id FROM Rit R INNER JOIN Meerijder M ON R.rit_id = M.rit_id WHERE M.lid_id = " + currentLid.getLidId() + " AND R.rit_datum >= current_date AND M.optie_geaccepteerd = 0 ORDER BY rit_datum asc, rit_tijd asc";
        List<Integer> Ritnummers = session.createSQLQuery(sqlquery).list();

        if (Ritnummers.size() > 0) {
            for (int i = 0; i < Ritnummers.size(); i++) {
                Rit tempRit = (Rit) session.load(Rit.class, new Integer(Ritnummers.get(i)));
                Ritten.add(tempRit);
            }
        }

        sqlquery = "SELECT R.rit_id FROM Rit R INNER JOIN Meerijder M ON R.rit_id = M.rit_id WHERE M.lid_id = " + currentLid.getLidId() + " AND R.rit_datum >= current_date AND M.optie_geaccepteerd = 1 ORDER BY rit_datum asc, rit_tijd asc";
        Ritnummers = session.createSQLQuery(sqlquery).list();

        if (Ritnummers.size() > 0) {
            for (int i = 0; i < Ritnummers.size(); i++) {
                Rit tempRit = (Rit) session.load(Rit.class, new Integer(Ritnummers.get(i)));
                AcceptedRitten.add(tempRit);
            }
        }

        request.setAttribute("ritten", Ritten);
        request.setAttribute("aantalRitten", Ritten.size());
        request.setAttribute("accRitten", AcceptedRitten);
        request.setAttribute("aantalAccRitten", AcceptedRitten.size());

        String address = "/mijnInschrijvingen.jsp";
        RequestDispatcher dispatcher = request.getRequestDispatcher(address);
        dispatcher.forward(request, response);
    }

    private String GetGoogleString(String Plaats, String Straat) {
        if (Straat == null) {
            return Plaats;
        } else {
            return Plaats + "," + Straat;
        }
    }
}